package com.umeox.iqibla.qimi_app.models

import org.json.JSONArray
import org.json.JSONObject


data class DeviceInfo(
    val version: Int,
    val flash_size: Int,
    val psram_size: Int,
    val minimum_free_heap_size: Int,
    val mac_address: String,
    val uuid: String,
    val chip_model_name: String,
    val chip_info: ChipInfo,
    val application: Application,
    val partition_table: List<Partition>,
    val ota: OTA,
    val board: Board
)

fun DeviceInfo.toJson() = JSONObject().apply {
    put("version", version)
    put("flash_size", flash_size)
    put("psram_size", psram_size)
    put("minimum_free_heap_size", minimum_free_heap_size)
    put("mac_address", mac_address)
    put("uuid", uuid)
    put("chip_model_name", chip_model_name)
    put("chip_info", JSONObject().apply {
        put("model", chip_info.model)
        put("cores", chip_info.cores)
        put("revision", chip_info.revision)
        put("features", chip_info.features)
    })
    put("application", JSONObject().apply {
        put("name", application.name)
        put("version", application.version)
        put("compile_time", application.compile_time)
        put("idf_version", application.idf_version)
        put("elf_sha256", application.elf_sha256)
    })
    put("partition_table", JSONArray(partition_table.map {
        JSONObject().apply {
            put("label", it.label)
            put("type", it.type)
            put("subtype", it.subtype)
            put("address", it.address)
            put("size", it.size)
        }
    }))
    put("ota", JSONObject().apply {
        put("label", ota.label)
    })
    put("board", JSONObject().apply {
        put("name", board.name)
        put("revision", board.revision)
        put("features", JSONArray(board.features))
        put("manufacturer", board.manufacturer)
        put("serial_number", board.serial_number)
    })
}.toString()